import pandas as pd
import clickhouse_driver
import pymysql

from sqlalchemy import create_engine

class mysql_db:
    def __init__(self, database:str):
        user="user_rpa"
        password="123456"
        # host="192.168.8.10" # 西安数据库
        host="10.20.30.1" # 河津数据库
        port=3306
        if not database:
            raise("请输入数据库")
        self.engine = create_engine(f'mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4')
        self.conn = pymysql.connect(user=user, password=password, host=host, port=port, database=database, charset='utf8mb4')
        self.cur = self.conn.cursor()

    def read_data(self, sql):
        query = pd.read_sql(sql, con=self.engine)
        return query

class click_db:
    def __init__(self):
        self.client = clickhouse_driver.Client(
            # host="192.168.8.10",# 西安运行时，使用该host
            host="10.20.30.1", # 河津运行时，使用该host
            port=9000,
            user="default",
            password="123456",
            database="keyword"
        )
    
    def read_data(self, sql):
        data, columns = self.client.execute(sql, columnar=True, with_column_types=True)
        df = pd.DataFrame({col[0]: d for d, col in zip(data, columns)})
        return df